package com.zhc.dejingapi.service.upload.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhc.dejingapi.entity.BomworkEntity;
import com.zhc.dejingapi.entity.QingMoordermainEntity;
import com.zhc.dejingapi.mapper.*;
import com.zhc.dejingapi.pojo.erp.BomworkMainModel;
import com.zhc.dejingapi.pojo.erp.BomworkMainUpdModel;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataResultMain;
import com.zhc.dejingapi.pojo.qingflow.queryresult.QingQueryActionResultBase;
import com.zhc.dejingapi.service.upload.IGaogeErpDataWithDetailUploadService;
import com.zhc.dejingapi.utils.PubInfoUtils;
import com.zhc.dejingapi.utils.qing.BomWorkUploadUtils;
import com.zhc.dejingapi.utils.qing.QingflowAPIUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class GaogeErpDataWithDetailUploadServiceImpl implements IGaogeErpDataWithDetailUploadService {

    //系统能用对象mapper

    //region 系统能用对象mapper
    @Autowired
    private QingAppfieldsMapper qingAppfieldsMapper;
    @Autowired
    private QingAppinfoMapper qingAppinfoMapper;

    @Autowired
    private QingAppfieldtablevaluesMapper qingAppfieldtablevaluesMapper;
    //endregion


    private final JdbcTemplate mysqlJdbcTemplate;
    @Autowired
    QingflowApiInfo qqingflowApiInfo;

    @Autowired
    private BomworkMapper bomworkMapper;

    @Autowired
    private QingMoordermainMapper qingMoordermainMapper;
    @Autowired
    private QingMoorderroutingMapper qingMoorderroutingMapper;

    private final RedisTemplate<String,Object> redisTemplate;


    public GaogeErpDataWithDetailUploadServiceImpl(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mysqlJdbcTemplate1,
                                                   @Qualifier("redisTemplateWin")RedisTemplate<String, Object> redisTemplate)
    {
        this.mysqlJdbcTemplate = mysqlJdbcTemplate1;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public boolean uploadBomWork() {

        String strappKey="7m7ph33k1c01";

        //新增的时候

        PubInfoUtils.LoadMakeList(mysqlJdbcTemplate);

        String strTableName="bomwork";
        boolean bSuccess=false;
        Map<String,String> mapContrastFields=new HashMap<>();


        //数据来源 直接下发
        mapContrastFields.put("数据来源","orderSouceType");//数据来源  直接下发
        mapContrastFields.put("客户名称","cusName");//客户名称  cusName
        mapContrastFields.put("派工日期","sdate");//派工日期  sdate
        mapContrastFields.put("批次号","vpno");//批次号  vpno
        mapContrastFields.put("客户代号","cusCode");//客户代号  cusCode
        mapContrastFields.put("批号日期","vpnoLast");//批号日期  vpnoLast
        mapContrastFields.put("原批次号","vpno");//原批次号  vpno
        mapContrastFields.put("标识码","eri");//标识码  eri
        mapContrastFields.put("工艺路线","proutingName");//工艺路线  proutingName
        mapContrastFields.put("计划结束时间","duedate");//计划结束时间  duedate
        mapContrastFields.put("计划开始时间","begindate");//计划开始时间  begindate
        mapContrastFields.put("计划产量","qty");//计划产量  qty
        mapContrastFields.put("产品编号","mprod");//产品编号  mprod
        mapContrastFields.put("预计天数","needDays");//预计天数  needDays
        mapContrastFields.put("规格型号","invStd");//规格型号  invStd
        //mapContrastFields.put("工艺路线","#Qing_ObjectTableValues#Routing");//工艺路线  #Qing_ObjectTableValues#Routing
        mapContrastFields.put("产品名称","invName");//产品名称  invName
        mapContrastFields.put("订单编号","code");//订单编号  code
        mapContrastFields.put("工单备注","memo");//工单备注  memo
        //mapContrastFields.put("产品图片","invPIC");//产品图片  invPIC
        //mapContrastFields.put("产品SOP","invSOP");//产品SOP  invSOP
        mapContrastFields.put("制造单位","maker");




        Map<String,String> mapContrastDetailFields=new HashMap<>();
        mapContrastDetailFields.put("计划数","opQty");//计划数  opQty
        mapContrastDetailFields.put("报工数配比","opRatio");//报工数配比  opRatio
        mapContrastDetailFields.put("是否为首道工序","isFirstFlag");//是否为首道工序  isFirstFlag
        mapContrastDetailFields.put("状态","detailRowStatus");//状态  detailRowStatus
        mapContrastDetailFields.put("是否为末道工序","isLastFlag");//是否为末道工序  isLastFlag
        mapContrastDetailFields.put("工序名称","opName");//工序名称  opName
        mapContrastDetailFields.put("工序编号","opCode");//工序编号  opCode
        mapContrastDetailFields.put("批次号","proBatchCode");//批次号  proBatchCode
        mapContrastDetailFields.put("计划开始时间","planStartTime");//计划开始时间  planStartTime
        mapContrastDetailFields.put("计划结束时间","planEndTime");//计划结束时间  planEndTime
        mapContrastDetailFields.put("工序行号","opSeq");//工序行号  opSeq
        mapContrastDetailFields.put("备注","detailmemo");//备注  detailmemo
        mapContrastDetailFields.put("机台","machineCode");//机台  machineCode
        mapContrastDetailFields.put("模具编号","moldCode");//模具编号  moldCode



        BomWorkUploadUtils UploadUtils =null;
        try{
            UploadUtils = new BomWorkUploadUtils(this.qqingflowApiInfo,
                    this.mysqlJdbcTemplate,this.qingAppfieldsMapper,
                    this.qingAppfieldtablevaluesMapper,
                    mapContrastFields,
                    mapContrastDetailFields,
                    strTableName);
        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace());
            return  false;
        }

        if(UploadUtils==null){
            return  false;
        }

        BomworkEntity entity =null  ;

        BomworkMapper mapper=bomworkMapper;

        try {

            List<BomworkEntity> listNoUpdate =null  ;

            //通过 SQL 将不需要的设置其他状态
            String strSql="UPDATE  bomwork a\n" +
                    "LEFT JOIN product_info b on a.MPROD =b.invCode\n" +
                    "set a.isNeedUpd=0 \n" +
                    "where  (a.lastRequestId IS NULL or a.lastRequestId='')\n" +
                    " AND (a.isWaiting=0  or a.isWaiting is NULL ) \n" +
                    " and a.isNeedUpd=1\n" +
                    " AND (a.applyId IS NULL OR a.applyId=0)\n" +
                    " and (NOT  b.invClass like 'ZS%' )\n" +
                    " ";


            mysqlJdbcTemplate.update(strSql);



            //region 新增初始行
            QueryWrapper<BomworkEntity> addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where (lastRequestId IS NULL or lastRequestId='')" +
                    "  AND (isWaiting=0  or isWaiting is NULL ) and isNeedUpd=1" +
                    "  AND (applyId IS NULL OR applyId=0)" +
                    "   ");//and invClass LIKE 'ZS%'
            //and mprod in (SELECT invCode from product_info where invClass like 'ZS%' )

            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    if (entity.getEri()==null||entity.getEri().isEmpty()){

                    }
                    else {

                        try {
                            BomworkMainModel bomworkMainModel=new BomworkMainModel();
                            bomworkMainModel.Copy(entity);
                            bomworkMainModel.LoadRouting(this.mysqlJdbcTemplate);
                            bomworkMainModel.LoadInvExtendInfo(this.mysqlJdbcTemplate);
                            bomworkMainModel.setOrderSouceType("未开始");


                            QingAddDataResultMain resultMain = UploadUtils.AddEntity(bomworkMainModel);
                            //region 处理返回结果
                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("A");
                                    mapper.updateById(entity);

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion
                        } catch (Exception e) {
                            e.printStackTrace();;

                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }
                    }
                }
            }
            //endregion

            //清除不需要上传的内容
            strSql="\n" +
                    "UPDATE  bomwork a\n" +
                    "LEFT JOIN product_info b on a.MPROD =b.invCode\n" +
                    " set a.isNeedUpd=0 \n"+
                    "where  (a.lastRequestId IS NULL or a.lastRequestId='')\n" +
                    " AND (a.isWaiting=0  or a.isWaiting is NULL ) \n" +
                    " and a.isNeedUpd=1\n" +
                    " and a.updAction='A'\n" +
                    " and (NOT  b.invClass like 'ZS%' )";
            mysqlJdbcTemplate.update(strSql);


            //region 新增行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='A'" +
                    " AND (isWaiting=0  or isWaiting is NULL )  and isNeedUpd=1 " +
                    " and applyId>0 " +
                    "   ");//and invClass LIKE 'ZS%'
            //and mprod in (SELECT invCode from product_info where invClass like 'ZS%' )


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {


                    try {
                        entity = listNoUpdate.get(i);
                        BomworkMainModel bomworkMainModel=new BomworkMainModel();
                        bomworkMainModel.Copy(entity);
                        bomworkMainModel.LoadRouting(this.mysqlJdbcTemplate);
                        bomworkMainModel.LoadInvExtendInfo(this.mysqlJdbcTemplate);
                        bomworkMainModel.setOrderSouceType("未开始");

                        QingAddDataResultMain resultMain= UploadUtils.AddEntity(bomworkMainModel);
                        //region 处理返回结果
                        if (resultMain == null) {

                            entity.setErrCode("-10015");
                            entity.setErrMsg("服务器返回内容为空！");
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            entity.setDealDate(LocalDateTime.now());
                            mapper.updateById(entity);

                        } else {
                            if (resultMain.getErrCode() == 0) {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg("提交成功！");
                                entity.setLastRequestId(resultMain.getResult().getRequestId());
                                entity.setIsDeal(0);
                                entity.setIsWaiting(1);
                                entity.setDealDate(LocalDateTime.now());
                                entity.setActionResult("A");
                                mapper.updateById(entity);

                            } else {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg(resultMain.getErrMsg());
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);
                            }

                        }
                        //endregion
                    } catch (Exception e) {
                        entity.setErrCode("-10076");
                        entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        entity.setLastRequestId(null);
                        entity.setIsDeal(0);
                        entity.setIsWaiting(0);
                        mapper.updateById(entity);
                    }

                }
            }
            //endregion

            //修改

            //执行SQL，将不合格的清除掉，是不可行？
            //清除不需要上传的内容


            mapContrastFields=new HashMap<>();


            //数据来源 直接下发
            mapContrastFields.put("数据来源","moOrderSouce");//“&E44&”
            mapContrastFields.put("编号","qingRowCode");//“&E44&”
            mapContrastFields.put("客户名称","cusName");//“&E44&”
            mapContrastFields.put("申请人","qingApplyUser");//“&E44&”
            mapContrastFields.put("申请时间","qingApplyDate");//“&E44&”
            mapContrastFields.put("更新时间","qingUpdateDate");//“&E44&”
            mapContrastFields.put("派工日期","dispDate");//“&E44&”
            mapContrastFields.put("当前流程状态","qingRowState");//“&E44&”
            mapContrastFields.put("批次号","proBatchNo");//“&E44&”
            mapContrastFields.put("客户代号","cusCode");//“&E44&”
            mapContrastFields.put("工单状态","moStatus");//“&E44&”
            mapContrastFields.put("批号日期","proBatchDateCode");//“&E44&”
            mapContrastFields.put("原批次号","orgProBatch");//“&E44&”
            mapContrastFields.put("标识码","moERI");//“&E44&”
            mapContrastFields.put("工艺路线","routingName");//“&E44&”
            mapContrastFields.put("计划结束时间","planStartDate");//“&E44&”
            mapContrastFields.put("计划开始时间","planEndDate");//“&E44&”
            mapContrastFields.put("机台","eqCode");//“&E44&”
            mapContrastFields.put("模具编号","moldCode");//“&E44&”
            mapContrastFields.put("色号","colorCode");//“&E44&”
            mapContrastFields.put("计划产量","proQty");//“&E44&”
            mapContrastFields.put("产品编号","invCode");//“&E44&”
            mapContrastFields.put("预计天数","planDays");//“&E44&”
            mapContrastFields.put("规格型号","invStd");//“&E44&”
            mapContrastFields.put("产品名称","invName");//“&E44&”
            mapContrastFields.put("订单编号","moCode");//“&E44&”
            mapContrastFields.put("工单备注","moMemo");//“&E44&”
            mapContrastFields.put("产品图片","invPIC");//“&E44&”
            mapContrastFields.put("产品SOP","invSOP");//“&E44&”
            mapContrastFields.put("制造单位","makerUnit");//“&E44&”
            mapContrastFields.put("状态","bomMakerState");//“&E44&”





            mapContrastDetailFields=new HashMap<>();
            mapContrastDetailFields.put("工序行号","opSeq");//工序行号
            mapContrastDetailFields.put("工序编号","opCode");//工序编号
            mapContrastDetailFields.put("工序名称","opName");//工序名称
            mapContrastDetailFields.put("计划数","opQty");//计划数
            mapContrastDetailFields.put("报工数配比","opRatio");//报工数配比
            mapContrastDetailFields.put("批次号","proBatchCode");//批次号
            mapContrastDetailFields.put("是否为首道工序","isFirstFlag");//是否为首道工序
            mapContrastDetailFields.put("是否为末道工序","isLastFlag");//是否为末道工序
            mapContrastDetailFields.put("状态","detailRowStatus");//状态
            mapContrastDetailFields.put("备注","detailmemo");//备注
            mapContrastDetailFields.put("计划开始时间","planStartTime");//计划开始时间
            mapContrastDetailFields.put("计划结束时间","planEndTime");//计划结束时间
            mapContrastDetailFields.put("机台","machineCode");//机台
            mapContrastDetailFields.put("模具编号","moldCode");//模具编号
            mapContrastDetailFields.put("剩余未清数","leaveQty");//剩余未清数
            mapContrastDetailFields.put("已完成数","finishQty");//已完成数
            mapContrastDetailFields.put("生产任务单号","motaskCode");//生产任务单号

            //生产人员

            BomWorkUploadUtils bomWorkUpdateUtils=new BomWorkUploadUtils(this.qqingflowApiInfo,
                    this.mysqlJdbcTemplate,this.qingAppfieldsMapper,
                    this.qingAppfieldtablevaluesMapper,
                    mapContrastFields,
                    mapContrastDetailFields,
                    strTableName);

            strSql="\n" +
                    "UPDATE  bomwork a\n" +
                    "LEFT JOIN product_info b on a.MPROD =b.invCode\n" +
                    " set a.isNeedUpd=0 \n"+
                    "where  (a.lastRequestId IS NULL or a.lastRequestId='')\n" +
                    " AND (a.isWaiting=0  or a.isWaiting is NULL ) \n" +
                    " and a.isNeedUpd=1\n" +
                    " and a.updAction='M'\n" +
                    " and (NOT  b.invClass like 'ZS%' ) ";
            mysqlJdbcTemplate.update(strSql);

            //已经开始生产的就需要无法修改
            strSql="\n" +
                    " UPDATE bomwork a\n" +
                    "LEFT JOIN qing_moordermain b on a.ERI=b.moERI\n" +
                    " set isNeedUpd=0 \n"+
                    "where  (a.lastRequestId IS NULL or a.lastRequestId='')\n" +
                    " AND (a.isWaiting=0  or a.isWaiting is NULL ) \n" +
                    " and a.isNeedUpd=1\n" +
                    " and a.updAction='M'\n" +
                    " and b.moStatus!='未开始'";
            mysqlJdbcTemplate.update(strSql);

            //region 修改行

            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') " +
                    " and updAction='M'  and isNeedUpd=1 " +
                    " AND (isWaiting=0  or isWaiting is NULL )  " +
                    " and applyId>0 " +
                    " ");//and invClass LIKE 'ZS%'


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    BomworkMainUpdModel bomworkMainModel=new BomworkMainUpdModel();




                    //获取现有信息？

                    //A 表示存档D 表示已经删除

                    try {
                        QueryWrapper<QingMoordermainEntity> updQueryWrapper=new QueryWrapper<>();
                        updQueryWrapper.eq("moERI",entity.getEri());
                        QingMoordermainEntity moordermainEntity=qingMoordermainMapper.selectOne(updQueryWrapper);

                        if (moordermainEntity==null){

                            throw new Exception("未找到当前订单下载后的信息，qing_moordermain 未到找相关信息");
                        }
                        bomworkMainModel.Copy(moordermainEntity);
                        //BeanUtils.copyProperties(bomworkMainModel, moordermainEntity);

                        bomworkMainModel.setUpdAction(entity.getUpdAction());
                        bomworkMainModel.setApplyId(entity.getApplyId());
                        bomworkMainModel.setProQty((int)Math.round(entity.getQty()));
                        //bomworkMainModel.LoadInvExtendInfo(this.mysqlJdbcTemplate);
                        bomworkMainModel.LoadRouting(this.mysqlJdbcTemplate,qingMoordermainMapper,qingMoorderroutingMapper);




                        QingAddDataResultMain resultMain=  bomWorkUpdateUtils.UpdEntity(bomworkMainModel,"A");
                        //region 处理返回结果
                        if (resultMain == null) {

                            entity.setErrCode("-10015");
                            entity.setErrMsg("服务器返回内容为空！");
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            entity.setDealDate(LocalDateTime.now());
                            mapper.updateById(entity);

                        } else {
                            if (resultMain.getErrCode() == 0) {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg("提交成功！");
                                entity.setLastRequestId(resultMain.getResult().getRequestId());
                                entity.setIsDeal(0);
                                entity.setIsWaiting(1);
                                entity.setDealDate(LocalDateTime.now());
                                entity.setActionResult("A");
                                mapper.updateById(entity);

                            } else {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg(resultMain.getErrMsg());
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);
                            }

                        }
                        //endregion
                    } catch (Exception e) {
                        e.printStackTrace();
                        entity.setErrCode("-10076");
                        entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        entity.setLastRequestId(null);
                        entity.setIsDeal(0);
                        entity.setIsWaiting(0);
                        mapper.updateById(entity);
                    }

                }
            }
            //endregion

            //删除条件
            strSql="\n" +
                    "UPDATE  bomwork a\n" +
                    "LEFT JOIN product_info b on a.MPROD =b.invCode\n" +
                    " set a.isNeedUpd=0 \n"+
                    "where  (a.lastRequestId IS NULL or a.lastRequestId='')\n" +
                    " AND (a.isWaiting=0  or a.isWaiting is NULL ) \n" +
                    " and a.isNeedUpd=1\n" +
                    " and a.updAction='D'\n" +
                    " and (NOT  b.invClass like 'ZS%' ) ";
            mysqlJdbcTemplate.update(strSql);

            //region 删除行

            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') " +
                    "and updAction='D' AND (isWaiting=0  or isWaiting is NULL ) " +
                    " and applyId>0" +
                    "   ");//and invClass LIKE 'ZS%'


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "D"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    //判断有没有使用，如果使用，只能修改，否则调删除
                    strSql="SELECT COUNT(*)\n" +
                            " from bomwork \n" +
                            " WHERE MPROD=? ";

                    int iHaveCount=0;//mysqlJdbcTemplate.queryForObject(strSql,new Object[]{entity.getInvCode()},Integer.class);

                    if(iHaveCount>0){
                        try {
                            BomworkMainUpdModel bomworkMainModel=new BomworkMainUpdModel();
                            QueryWrapper<QingMoordermainEntity> updQueryWrapper=new QueryWrapper<>();
                            updQueryWrapper.eq("moERI",entity.getEri());
                            QingMoordermainEntity moordermainEntity=qingMoordermainMapper.selectOne(updQueryWrapper);

                            if (moordermainEntity==null){

                                throw new Exception("未找到当前订单下载后的信息，qing_moordermain 未到找相关信息");
                            }
                            bomworkMainModel.Copy(moordermainEntity);
                            //BeanUtils.copyProperties(bomworkMainModel, moordermainEntity);

                            bomworkMainModel.setUpdAction(entity.getUpdAction());
                            bomworkMainModel.setApplyId(entity.getApplyId());
                            bomworkMainModel.setProQty((int)Math.round(entity.getQty()));
                            //bomworkMainModel.LoadInvExtendInfo(this.mysqlJdbcTemplate);
                            bomworkMainModel.LoadRouting(this.mysqlJdbcTemplate,qingMoordermainMapper,qingMoorderroutingMapper);




                            QingAddDataResultMain resultMain=  bomWorkUpdateUtils.UpdEntity(bomworkMainModel,"A");
                            //region 处理返回结果
                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("A");
                                    mapper.updateById(entity);

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion
                        } catch (Exception e) {
                            e.printStackTrace();
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }


                    }
                    else{
                        try {


                            QingAddDataResultMain resultMain = UploadUtils.DelEntity(entity);

                            //region 处理返回结果

                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("D");
                                    mapper.updateById(entity);
                                    //增加马上判断的结果

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion

                        } catch (Exception e) {
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }
                    }

                }
            }
            //endregion


        }
        catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage()+"\n"+e.getStackTrace());
        }



        return true;

    }

    @Override
    public boolean QueryBomWorkActionResult() {

        QueryWrapper<BomworkEntity> queryWrapper=new QueryWrapper<>();
        BomworkEntity entity =null  ;

        BomworkMapper mapper=bomworkMapper;
        queryWrapper=null;
        queryWrapper=new QueryWrapper<>();

        //queryWrapper.eq("isDeal",0)
        //        .and(wq->wq.eq("lastRequestId","")
        //                .or().isNull("lastRequestId")
        //        );
        queryWrapper.last(" where isDeal=0 and isWaiting=1 and NOT (lastRequestId is NULL or lastRequestId='')");

        List<BomworkEntity> listQuery=mapper.selectList(queryWrapper);
        if (listQuery==null||listQuery.isEmpty()){

        }
        else {
            for (int i = 0; i < listQuery.size(); i++) {
                if(listQuery.get(i).getLastRequestId()==null||listQuery.get(i).getLastRequestId().isEmpty()){

                }
                else {
                    entity=listQuery.get(i);
                    try{
                        QingQueryActionResultBase actionResultBase= QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,listQuery.get(i).getLastRequestId());
                        if (actionResultBase==null){
                            entity.setErrCode("-10066");
                            entity.setErrMsg("返回出错：返回内容为空！");
                            mapper.updateById(entity);
                        }
                        else{
                            if (actionResultBase.getErrorCode()==0){
                                if (entity.getActionResult().equals("A")){
                                    entity.setErrCode("0");
                                    entity.setErrMsg("更新成功！");
                                    entity.setLastRequestId(null);
                                    entity.setIsWaiting(0);
                                    entity.setIsNeedUpd(0);
                                    //entity.setUpdAction("");//清除
                                    entity.setIsDeal(1);
                                    try {
                                        long applyId= Long.parseLong(actionResultBase.getResult());
                                        entity.setApplyId(applyId);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }
                                else if (entity.getActionResult().equals("D")){

                                    try {
                                        mapper.deleteById(entity);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }

                                mapper.updateById(entity);
                            }
                            else{
                                entity.setErrCode(actionResultBase.getErrorCode()+"");
                                entity.setErrMsg(actionResultBase.getMessage());
                                entity.setLastRequestId(null);
                                entity.setIsWaiting(0);
                                mapper.updateById(entity);
                            }
                        }
                    }
                    catch (Exception e){
                        entity.setErrCode("-10076");
                        entity.setErrMsg("返回出错："+e.getMessage()+"\n"+e.getStackTrace());
                        mapper.updateById(entity);
                    }

                }
            }
        }

        return true;
    }
}
